package com.kcwl.carrier.infrastructure.repository.persostence;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kcwl.carrier.domain.entity.OperateLog;
import com.kcwl.carrier.domain.repository.IOperateLogRepository;
import com.kcwl.carrier.infrastructure.repository.mapper.OperateLogMapper;
import com.kcwl.carrier.infrastructure.repository.po.OperateLogPO;
import com.kcwl.carrier.interfaces.enums.OperateLogTypeEnum;
import com.kcwl.carrier.interfaces.query.OperateLogQuery;
import com.kcwl.ddd.domain.entity.KcPage;
import com.kcwl.ddd.domain.repository.BaseRepositoryImpl;
import com.kcwl.framework.utils.KcBeanConverter;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author 刘旭超
 * @date 2022年07月11日 16:33
 * @Description
 */
@Service
public class OperateLogRepositoryImpl extends BaseRepositoryImpl<OperateLogMapper, OperateLogPO> implements IOperateLogRepository {

    @Resource
    private OperateLogMapper operateLogMapper;

    @Override
    public List<OperateLog> queryLog(OperateLogQuery operateLogQuery) {
        QueryWrapper<OperateLogPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("log_type", operateLogQuery.getType());
        OperateLogTypeEnum operateLogTypeEnum = OperateLogTypeEnum.getByType(operateLogQuery.getType());
        queryWrapper.eq(operateLogTypeEnum.getTableFiled(), operateLogQuery.getId());
        queryWrapper.orderByAsc("submit_time");
        List<OperateLogPO> operateLogPOList = operateLogMapper.selectList(queryWrapper);
        return KcBeanConverter.toList(operateLogPOList, OperateLog.class);
    }

    @Override
    public int insertLog(OperateLog operateLog) {
        OperateLogPO insert = KcBeanConverter.toBean(operateLog, OperateLogPO.class);
        insert.setSubmitTime(new Date());
        return operateLogMapper.insert(insert);
    }

    @Override
    public Class<OperateLogPO> getPoClass() {
        return OperateLogPO.class;
    }

}
